package com.company.Sort2;

import java.util.Arrays;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}9:52
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] arr = {8,1,9,6,4,3,-1};
        System.out.println(Arrays.toString(insertSort(arr)));
    }

    public static int[] insertSort(int[] arr){
        for (int i = 0;i < arr.length; i++) {
            int indexValue = arr[i];
            int insertIndex = i - 1;
            while (insertIndex >= 0 && indexValue < arr[insertIndex]) {
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            arr[insertIndex + 1] = indexValue;
        }
        return arr;
    }
}
